﻿@{
    ViewBag.Title = "DefaultUpdate";
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>学生修改</title>

    <script src="~/libs/jquery/jquery.js"></script>
    <link href="//unpkg.com/layui@2.9.16/dist/css/layui.css" rel="stylesheet">
    <script src="//unpkg.com/layui@2.9.16/dist/layui.js"></script>

</head>
<body>
    <h1>学生修改</h1>
    <form class="layui-form layui-form-pane" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">学生姓名</label>
            <div class="layui-input-block">
                <input type="text" name="name" id="name" required autocomplete="off" placeholder="请输入" lay-verify="required" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" pane>
            <label class="layui-form-label">年龄</label>
            <div class="layui-input-block">
                <input type="text" name="passage" id="passage" required autocomplete="off" placeholder="请输入" lay-verify="required" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <button class="layui-btn" type="button" lay-submit lay-filter="btnAdd">确认</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </form>

    <script>
        layui.use(['form', 'jquery', 'element'], function () {
            var form = layui.form,
                $ = layui.jquery;
            $ = layui.$;
            // 监听确认按钮点击事件





            $(document).ready(function () {

                getFormData();
            });

            function getFormData() {

                console.log(sessionStorage.getItem("id")),
                    $.ajax({
                        url: "/Wyx/RestoreWangUser?id=" + sessionStorage.getItem("id"),
                        //contentType: 'application/json',
                        dataType: "json",
                        type: "GET",
                        success: function (res) {
                            console.log(res);
                            if (res && res.data && res.data.length > 0) {
                                // 确保从返回数据中正确获取值
                                var nameValue = res.data[0].Name || ''; // 如果没有值则用空字符串
                                var passageValue = res.data[0].Passage || ''; // 如果没有值则用空字符串

                                $("#name").val(nameValue);
                                $("#passage").val(passageValue);
                            } else {
                                console.error("没有获取到有效数据");
                            }
                        }

                    });
            }





            form.on('submit(btnAdd)', function (updatedUser) {
                // 获取表单数据
                var id = sessionStorage.getItem("id");


                // 发送 AJAX 请求
                $.ajax({
                    url: '/Home/UpdateWangUser', // 根据路由修改为实际路径
                    type: 'GET', // 请求类型
                    contentType: 'application/json', // 设置请求内容类型为JSON
                    data: { id: id, name: $('#name').val(), passage: $('#passage').val(), },
                    success: function (res) {

                        if (res) {
                            console.log(res);
                            layer.msg('修改成功', {
                                icon: 1,
                                time: 1000
                            }, function () {
                                location.herf = '/';
                            });
                        }
                    },
                    error: function (xhr, status, error) {
                        console.error(error); // 打印错误信息
                        alert('请求失败: ' + error); // 提示请求失败
                    }
                });
            });


        });
    </script>

</body>
</html>
